Clear-Host
Write-Host -Object '******************************'
Write-Host -Object '* Log Off User Remotely *'
Write-Host -Object '******************************'
Write-Host
$global:adminCreds = $host.ui.PromptForCredential('Need credentials', 'Please enter your user name and password.', '', '')
$global:ComputerName = Read-Host -Prompt 'Computer Name?'
Function get-usersessions
{
Write-Host
Write-Host -Object 'Getting user sessions...'
Write-Host
Write-Host -Object '***************************************************************************'
Invoke-Command -ComputerName $global:ComputerName -ScriptBlock {
query.exe session
} -Credential $global:adminCreds
}
Function start-userlogoff
{
Write-Host
$SessionNum = Read-Host -Prompt 'Session ID number to log off?'
$title = 'Log Off'
$message = 'Are you sure you want to log them off?'
$yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes', 'Logs selected user off.'
$no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No', 'Exits.'
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 1)
switch ($result){
0
{
Write-Host
Write-Host -Object 'OK. Logging them off...'
Invoke-Command -ComputerName $global:ComputerName -ScriptBlock {
logoff.exe $args[0]
} -ArgumentList $SessionNum -Credential $global:adminCreds
Write-Host
Write-Host -Object 'Success!' -ForegroundColor green
break
}
1
{
break
}
}
}
Do
{
get-usersessions
start-userlogoff
Write-Host
#Write-Host "Press any key to continue ..."
# $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
#Configure yes choice
$yes = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes', 'Remove another profile.'
#Configure no choice
$no = New-Object -TypeName System.Management.Automation.Host.ChoiceDescription -ArgumentList '&No', 'Quit profile removal'
#Determine Values for Choice
$choice = [System.Management.Automation.Host.ChoiceDescription[]] @($yes, $no)
#Determine Default Selection
[int]$default = 0
#Present choice option to user
$userchoice = $host.ui.PromptforChoice('','Logoff Another Profile?',$choice,$default)
}
#If user selects No, then quit the script
Until ($userchoice -eq 1)